package cn.lazar.homework

import org.apache.spark.graphx.{Edge, Graph, VertexId}
import org.apache.spark.rdd.RDD
import org.apache.spark.{SparkConf, SparkContext}

object SparkGraphx {
  def main(args: Array[String]): Unit = {
    val conf: SparkConf = new SparkConf().setAppName(this.getClass.getCanonicalName)
      .setMaster("local[*]")
    val sc = new SparkContext(conf)
    sc.setLogLevel("warn")

    // 定义顶点
    val vertexArray: Array[(VertexId, (String))] = Array(
      (1L, ("SGO")),
      (2L, ("ORD")),
      (3L, ("DFW"))
    )

    val vertexRDD: RDD[(VertexId, (String))] = sc.makeRDD(vertexArray)

    // 定义边
    val edgeArray: Array[Edge[Int]] = Array(
      Edge(1L, 2L, 1800),
      Edge(2L, 3L, 800),
      Edge(3L, 1L, 1400)
    )
    val edgeRDD: RDD[Edge[Int]] = sc.makeRDD(edgeArray)

    // 图的定义
    val graph: Graph[(String), Int] = Graph(vertexRDD, edgeRDD)

    //求所有定点
    graph.vertices.foreach(println)
    //求所有边
    graph.edges.foreach(println)
    //求所有triplets
    graph.triplets.foreach(println)
    // 求顶点数
    println(graph.vertices.count())
    // 求边数
    println(graph.edges.count())
    //求距离大于1000的并且排序
    graph.edges.filter(edge=>edge.attr>1000).sortBy(_.attr,true).foreach(println)

  }
}
